from rest_framework.views import APIView
from utils.result import R
from api.models import ItEduVipOrder,ItEduLessonOrder

class VipOrderAPIView(APIView):

    """
    获取所有订单条数
    """
    def get(self,request,*args, **kwargs):

        user_pk = request.session.get("user_pk")

        total_num = ItEduVipOrder.objects.filter(user___id=user_pk).count()

        return R().data_result(total_num=total_num)

    """
    获取订单信息
    """
    def post(self,request,*args, **kwargs):

        user_pk = request.session.get("user_pk")
        offset = request.data.get("offset")
        limit = request.data.get("limit")

        if limit > 30:
            return R.params_error().to_json_response()

        vip_order_list = list(ItEduVipOrder.objects.filter(user___id=user_pk).order_by("-_id")[offset:offset+limit].values())

        return R().data_result(vip_order_list = vip_order_list)

class LessonOrderAPIView(APIView):

    """
    获取所有订单条数
    """
    def get(self,request,*args, **kwargs):

        user_pk = request.session.get("user_pk")

        total_num = ItEduLessonOrder.objects.filter(user___id=user_pk).count()

        return R().data_result(total_num=total_num)

    """
    获取订单信息
    """
    def post(self,request,*args, **kwargs):

        user_pk = request.session.get("user_pk")
        offset = request.data.get("offset")
        limit = request.data.get("limit")

        if limit > 30:
            return R.params_error().to_json_response()

        lesson_order_list = list(ItEduLessonOrder.objects.filter(user___id=user_pk).order_by("-_id")[offset:offset+limit].values(
            "createtime",
            "lesson__title",
            "status",
            "orderId",
            "lesson__price"
        ))

        return R().data_result(lesson_order_list = lesson_order_list)
